layui.use(['form', 'jquery', 'layer', 'table', 'laydate', 'laypage','upload'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var laypage = layui.laypage;
	var upload = layui.upload;
	var where_json = {
		_o: 'id',
		_b: 'desc'
	};

	var table_filter = 'month_contract_list',
		form_filter = 'month_contract_search_form';
	// 渲染表格
	table.render({
		elem: '#' + table_filter,
		id: table_filter,
		url: month_contract_list_url,
		method: 'post',
		height: 'full-200',
		where: {
			_o: 'id',
			_b: 'desc'
		},
		cols: [month_cols],
		page: {
			layout: ['count', 'first', 'prev', 'page', 'next', 'last', 'skip'],
			limit: 30,
			theme: '#393d49',
			groups: 8
		},
		autoSort: false,
		toolbar: '#monthContractToolbar',
		defaultToolbar: [],
		initSort: {
			field: 'id',
			type: 'desc'
		},
		done:()=>{
			console.log('完成')
		}
	});
	
	var up_sny_state = upload.render({
		elem: '.up-sny-state',
		url: sny_state_url,
		accept: 'file',
		acceptMime: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
		exts: 'xls',
		choose: function() {
			layer.load(1);
		},
		done: function(res) {
			console.log(res);
			layer.closeAll('loading');
			layer.msg('同步成功', {
				icon: 1,
				anim: 6
			});
			table.reload(table_filter);
		},
		error: function() {
			layer.closeAll('loading');
			layer.msg('上传错误', {
				icon: 2,
				anim: 6
			});
			return false;
		}
	});

	// 监听表头按钮
	table.on('toolbar(' + table_filter + ')', function(obj) {
		var checkStatus = table.checkStatus(table_filter);
		if (checkStatus.data.length == 0) {
			layer.msg('请选择', {
				icon: 2,
				anim: 6
			});
			return false;
		}
		id_arr = new Array();
		for (var i in checkStatus.data) {
			if (checkStatus.data[i].audit != 3) {
				layer.msg('存在已审核的数据', {
					icon: 2,
					anim: 6
				});
				return false;
			}
			id_arr.push(checkStatus.data[i].id);
		}
		switch (obj.event) {
			case 'allPass':
				layer.confirm('确定审核通过选中吗？', {
					yes: function() {
						layer.load(2);
						send_info(contract_all_pass_url, {
							id_arr: id_arr
						}, function(data) {
							layer.closeAll();
							layer.msg(data.msg, {
								icon: data.code,
								anim: 6
							});
							table.reload(table_filter);
						})
					}
				});
				break;
			case 'allReject':
				layer.prompt({
					'title': '请输入拒绝原因'
				}, function(value, index, elem) {
					value = value.trim();
					if (value == '') {
						layer.msg('请输入拒绝原因');
						return false;
					}
					layer.load(2);
					send_info(contract_all_refuse_url, {
						id_arr: id_arr,
						remark: value
					}, function(data) {
						layer.closeAll();
						layer.msg(data.msg, {
							icon: data.code,
							anim: 6
						});
						table.reload(table_filter);
					})
				})
				break;
			default:
				break;
		}
		return false;
	})


	form.render();

	var upload_file = upload.render({
		elem: '.upload_file',
		url: uploadOne,
		accept: 'file',
		acceptMime: 'application/pdf',
		exts: 'pdf',
		choose: function() {
			layer.load(1);
		},
		done: function(res) {
			send_info(upload_file_url,{id:$('input[name="upload_file_id"]').val(),file:res.data.save_path},()=>{
				layer.closeAll();
				layer.msg('上传成功',{icon:1,anim:6})
				table.reload(table_filter);
			})
		},
		error: function() {
			layer.closeAll('loading');
			layer.msg('上传错误', {
				icon: 2,
				anim: 6
			});
			return false;
		}
	});
	// 表格工具监听
	table.on('tool(' + table_filter + ')', function(obj) {
		var data = obj.data;
		var e = obj.event;
		var tr = obj.tr;
		var id = data.id;
		switch (e) {
			case 'upload_file':
			$('input[name="upload_file_id"]').val(id)
				layer.open({
					title:'上传文件',
					shade:0,
					type:1,
					content:$('#upload_box'),
					area:['300px','300px']
				})
				break;
			case 'pass':
				layer.confirm('确认审核通过吗？', (index) => {
					send_info(contract_pass_url, {
						id: data.id
					}, () => {
						layer.msg('操作成功', {
							icon: 1,
							anim: 6
						});
						table.reload(table_filter);
					})
				})
				break;
			case 'reject':
				layer.prompt({
					'title': '请输入驳回原因'
				}, function(value, index, elem) {
					value = value.trim();
					if (value == '') {
						layer.msg('请输入驳回原因');
						return false;
					}
					layer.load(2);
					send_info(contract_reject_url, {
						id: id,
						remark: value
					}, function(data) {
						layer.closeAll();
						layer.msg(data.msg, {
							icon: data.code,
							anim: 6
						});
						table.reload(table_filter);
					})
				})
				break;
			default:
				break;
		}
	})

	// 表单input 查询
	$('form[lay-filter="' + form_filter + '"]').on('input propertychange', 'input[type="text"]', function() {
		if ($(this).prop('comStart')) return;
		var value = $(this).val();
		var name = $(this).attr('name');
		where_json[name] = value;

	}).on('compositionstart', 'input[type="text"]', function() {
		$(this).prop('comStart', true);
	}).on('compositionend', 'input[type="text"]', function() {
		$(this).prop('comStart', false);
		$(this).trigger('input');
	})

	laydate.render({
		elem: '#month',
		type: 'month',
		btns: ['clear', 'confirm'],
		done: (value, date, endDate) => {
			where_json['month'] = value;
			layer.load();
			table.reload(table_filter, {
				where: where_json,
				page: {
					curr: 1
				},
				done: function() {
					layer.closeAll();
				}
			});
		}
	});

	form.on('select', function(data) {
		var value = data.value;
		var e = data.elem;
		var id = $(e).attr('id');
		where_json['audit'] = value;
		layer.load();
		table.reload(table_filter, {
			where: where_json,
			page: {
				curr: 1
			},
			done: function() {
				layer.closeAll();
			}
		});
	});


})